package Interface;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Controleur.Simulation;

public class MenuSimulationPanel extends JPanel implements ActionListener, ChangeListener
{
	public enum TypeAjout 
	{
		NOEUD,
		SEGMENT
	}
	
	private static final long serialVersionUID = 1L;
	private final JButton demarrerSimulation;
	private final JButton arreterSimulation;
	private final JSlider sliderVitesse;
	
	
	private Image imageDemarrer = null;
	private Image imagePause = null;
	private Image imageArreter = null;
	private final Simulation simulation;
	

	public MenuSimulationPanel(Simulation _simulation) 
	{
		this.simulation = _simulation;
		
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		setPreferredSize(new Dimension(1000, 70));
		try 
		{
			imageDemarrer = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("play.gif"));
			imagePause = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("pause.gif"));
			imageArreter = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("stop.gif"));
		}
		catch (IOException ex){}
			
		c.insets = new Insets(10, 10, 10, 10);
		demarrerSimulation = new JButton();
		//demarrerSimulation.setSize(10, 10);
		demarrerSimulation.addActionListener(this);
	    demarrerSimulation.setIcon(new ImageIcon(imageDemarrer));
	    c.gridx = 0;
	    c.gridy = 0;
	    panel.add(demarrerSimulation, c);
		
		arreterSimulation = new JButton();
		//arreterSimulation.setSize(10, 10);
		arreterSimulation.setIcon(new ImageIcon(imageArreter));
		arreterSimulation.addActionListener(this);
	    c.gridx = 1;
	    panel.add(arreterSimulation, c);
	    
	    sliderVitesse = new JSlider(JSlider.HORIZONTAL,
                0, 100, 1);
	    sliderVitesse.addChangeListener(this);
	    //Slider labels
	    sliderVitesse.setMajorTickSpacing(10);
	    sliderVitesse.setPaintTicks(true);
		sliderVitesse.setPaintLabels(true);
		
		//Create the label table
		Hashtable labelTable = new Hashtable();
		labelTable.put( new Integer( 1 ), new JLabel("Lent") );
		labelTable.put( new Integer( 99 ), new JLabel("Rapide") );
		sliderVitesse.setLabelTable( labelTable );
		
		c.gridx = 2;
	    panel.add(sliderVitesse, c);
	    
		
		add(panel, BorderLayout.NORTH);
	}


	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == demarrerSimulation){
			if(simulation.isRunning())
			{
				demarrerSimulation.setIcon(new ImageIcon(imageDemarrer));
				simulation.pause();
			}else{
				demarrerSimulation.setIcon(new ImageIcon(imagePause));
				simulation.start();
			}
		}
		if(e.getSource() == arreterSimulation){
			simulation.restart();
			demarrerSimulation.setIcon(new ImageIcon(imageDemarrer));
		}
	}


	@Override
	public void stateChanged(ChangeEvent arg0) 
	{
		if(arg0.getSource() == sliderVitesse){
			JSlider source = (JSlider)arg0.getSource();
		    if (!source.getValueIsAdjusting()) {
		        simulation.setVitesseVehicule((int)source.getValue());
		    }
		}
		
	}
	
}
